<?php
/**
 * Created by PhpStorm.
 * User: yzapple
 * Date: 2018/1/17
 * Time: 上午8:56
 */

namespace app\doctor\controller;

use think\Db;
use think\Request;

/**
 * 患者列表
 *
 */
class Patient extends Basecontroller
{
    protected $noNeedLogin = [];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Patient');

    }

    /**
     * 患者列表
     */
    public function index()
    {
        //搜索
        $request = Request::instance();
       // $currentDate_s_e = getthemonth(date("Y-m-d"));
        $pageQuery = [];
        $option['is_deleted'] = ['=', 1];
        //$option['doctor_id'] = $this->doctor->id;

        //默认年月日
       // $pageQuery['query']['start'] = $currentDate_s_e[0];

       // $pageQuery['query']['end'] = $currentDate_s_e[1];

        if ($request->isGet()) {
            $data = $request->param();

            if (isset($data['name']) && trim($data['name'])) {
                $option['name|document_num'] = array('like', '%' . trim($data['name']) . '%');
                $pageQuery['query']['name'] = trim($data['name']);

            }
            if (isset($data['start']) && $data['start'] !== '') {

                $last_date =  date('Y-m-d',strtotime($data['start']));
                $now_date =  date('Y-m-d',time());
                $sencond_data = $data['end'] ? $data['end'] : $now_date;
                $end =  date('Y-m-d',strtotime($sencond_data.'+1 day'));

                $option['create_time'] = array('between', ["{$last_date}", "{$end}"]);
                $pageQuery['query']['start'] = trim($data['start']);
                $pageQuery['query']['end'] = trim($data['end']);

            }
        }
        $this->assign('search', isset($pageQuery['query']) ? $pageQuery['query'] : '');
        $list = $this->model
            ->where($option)
            ->order('create_time desc')
            ->paginate(false, false, $pageQuery)->each(function ($item, $key) {
                $option['patient_id'] = $item['id'];
                $option['is_deleted'] = 1;
                $item['record_count'] = $this->getUserfamilycase_count($option);
                return $item;
            });
        // print_r($list);
        $this->assign('list', $list);


        return $this->fetch();

    }

    /**
     * 新建患者
     */
    public function add_patient()
    {


        return $this->fetch();
    }

    /**
     * 患者信息
     */
    public function patient_info()
    {
//        print_r($this->request->param());
//        die();
        $pid = $this->request->param('pid');
        //分页HLC
        $param = $this->request->param();
        $page = isset($param['page'])?$param['page'] : 1;
        if ($this->request->param('pid')) {
            $pid = $this->request->param('pid');

        }else
        if ($this->request->param('dcnum')) {
          $patient =  $this->getPatientID_Bydocnum($this->request->param('dcnum'));
            $pid = $patient['id'];
        }
        $patient_info = $this->getPatient_ById($pid);
        $option['patient_id'] = $patient_info['id'];
        $option['is_deleted'] = 1;

        $case_list = $this->getCase_list($option, $page);

//        $cc = array(array('3' => '4'), array('3' => '5'), array('2' => '5'));
//
//        $ddd = json_encode($cc);
//        $aaa = json_decode($ddd, true);

        $new_case_list = [];
        foreach ($case_list as $key => $value) {

            $patient_case_type = $this->getPatient_case_type($value['patient_case_type']);
            $value['patient_case_type_name'] = $patient_case_type['name'];
            $value['patient_part_lat'] = json_decode($value['patient_part_lat'], true);
            $new_case_list[] = $value;
        }

        $this->assign('case_list', $new_case_list);

        $this->assign('patient', $patient_info);

        return $this->fetch();

    }

    /**
     * 检查记录
     */
    public function check_detail()
    {
        $cid = $this->request->param('cid');
        //不知道几张报告 样式可能要改
//        $cc = array('手部'=>array('http://miweikufang.oss-cn-beijing.aliyuncs.com/201801/1515489325.jpeg','http://miweikufang.oss-cn-beijing.aliyuncs.com/201801/1515489325.jpeg'));
//        $cc2 = array('脚部'=>array('http://miweikufang.oss-cn-beijing.aliyuncs.com/201801/1515489325.jpeg','http://miweikufang.oss-cn-beijing.aliyuncs.com/201801/1515489325.jpeg'));

//$aaa=array('脚部'=>array('http://miweikufang.oss-cn-beijing.aliyuncs.com/201801/1515489325.jpeg','http://miweikufang.oss-cn-beijing.aliyuncs.com/201801/1515489325.jpeg'),'手部'=>array('http://miweikufang.oss-cn-beijing.aliyuncs.com/201801/1515489325.jpeg','http://miweikufang.oss-cn-beijing.aliyuncs.com/201801/1515489325.jpeg'));
//         var_dump(json_encode($aaa));

        //获取病人信息以及病例
        $this->getPatient_info_case($cid);
        //取出电子报告
        $this->getPatient_report($cid);
        //取出电子胶片
        $this->getPatient_film($cid);

        return $this->fetch();

    }

}